var FileUploader = (function () {
    /**
     *
     * @param selector 一个css选择器
     * @param config 配置
     * host 七牛host
     * url 文件上传接口
     * token 七牛token
     * auto 在选择文件后上传文件，默认true
     * accept 文件类型
     * onSelect 文件选择后触发，参数：files-已选择的文件列表
     * onUploadBegin 文件开始上传，参数：index-当前文件索引
     * onUploadComplete 文件上传完成 参数：key-文件,index-当前文件索引
     * onUploadError 文件上传错误 参数：index-当前文件索引
     *
     *
     * 公用方法
     * setTrigger 设置选择器
     * upload 上传文件
     * uploadIndex 上传制定索引的文件，如果某个文件上传错误，则可调用此方法
     */
    function FileUploader(selector, config) {
        this.config = {
            host: 'up.qiniu.com',
            url: 'http://upload.qiniu.com/',
            auto: true,
            accept: 'image/png,image/gif,image/jpeg'
        };
        if (config) {
            for (var k in config) {
                this.config[k] = config[k];
            }
        }
        this.index = 0;
        this.inputs = [];
        this.setTrigger(selector);
    }
    FileUploader.prototype.setTrigger = function (selector) {
        var wrapper = document.querySelectorAll(selector);
        if (wrapper.length == 0)
            return;
        this.addInput(wrapper);
    };
    FileUploader.prototype.addInput = function (wrapper) {
        this.index = 0;
        this.errorFiles = [];
        this.inputs.forEach(function (o) {
            try {
                o.remove();
            }
            catch (e) {
            }
        });
        this.inputs = [];
        for (var i = 0; i < wrapper.length; i++) {
            wrapper[i].style.position = 'relative';
            var input = document.createElement('input');
            input.type = 'file';
            input.style.position = 'absolute';
            input.style.left = '0';
            input.style.right = '0';
            input.style.top = '0';
            input.style.bottom = '0';
            input.style.zIndex = '999';
            input.style.opacity = '0';
            input.style.width = '100%';
            input.accept = this.config.accept;
            if (this.config && this.config.multi === true) {
                input.multiple = true;
            }
            input.addEventListener('change', function (evt) {
                this.files = evt.target.files;
                typeof this.config.onSelect == 'function' && this.config.onSelect(this.files);
                if (this.config.auto === true) {
                    this.upload();
                }
            }.bind(this));
            wrapper[i].appendChild(input);
        }
    };
    FileUploader.prototype.upload = function () {
        if (this.index > this.files.length - 1) {
            return;
        }
        this.doUpload(this.files[this.index], function (result) {
            typeof this.config.onUploadComplete == 'function' && this.config.onUploadComplete(result.data.key, this.index);
            this.index++;
            this.upload();
        }.bind(this), function () {
            typeof this.config.onUploadError == 'function' && this.config.onUploadError(this.index);
            this.index++;
            this.upload();
        }.bind(this));
        //本地预览
        // let reader = new FileReader();
        // reader.readAsDataURL(file);
        // reader.onload=function (evt) {
        // }.bind(this)
    };
    /**
     * 上传指定索引的文件
     * @param index
     */
    FileUploader.prototype.uploadIndex = function (index, success, fail) {
        this.doUpload(this.files[index], success, fail);
    };
    FileUploader.prototype.doUpload = function (file, success, fail) {
        var data = new FormData();
        data.append('token', this.config.token);
        data.append('file', file);
        data.append('key', new Date().getTime() + Math.ceil(Math.random() * 10000));
        typeof this.config.onUploadBeign == 'function' && this.config.onUploadBeign(this.index);
        axios({
            url: this.config.url,
            method: "post",
            data: data,
            headers: {
                'Content-Length': file.size
            }
        }).then(function (result) {
            typeof success == 'function' && success(result);
        }.bind(this)).catch(function () {
            typeof fail == 'function' && fail();
        }.bind(this));
    };
    return FileUploader;
}());
//# sourceMappingURL=file-uploader.js.map

var fileUploader={
    upload:function (files) {

    }
}